package io.github.junxworks.qt.modules.stock.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.github.junxworks.ep.core.Result;
import io.github.junxworks.ep.core.utils.PageUtils;
import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.qt.modules.stock.dto.SsTradeRecordDto;
import io.github.junxworks.qt.modules.stock.dto.StockTradeCondition;
import io.github.junxworks.qt.modules.stock.service.TradeService;
import io.github.junxworks.qt.modules.stock.vo.SsTradeRecordVo;

@RestController
@RequestMapping("/stock/trade")
public class TradeController {

	@Autowired
	private TradeService tradeService;

	@GetMapping("/records")
	public Result queryTradeList(StockTradeCondition condition) {
		PageUtils.setPage(condition);
		Result res =Result.ok(new PageInfo<SsTradeRecordVo>(tradeService.queryTradeList(condition)));
		res.addAttribute("stat", tradeService.statTradeList(condition));
		return res;
	}

	@GetMapping("/records/{id}")
	public Result queryTradeList(@PathVariable("id") Long id) {
		return Result.ok(tradeService.queryTradeDetail(id));
	}

	@PostMapping("/buy-records")
	public Result saveBuyRecord(@RequestBody SsTradeRecordDto dto) {
		return Result.ok(tradeService.saveBuyRecord(dto));
	}

	@PostMapping("/records")
	public Result saveSellRecord(@RequestBody SsTradeRecordDto dto) {
		return Result.ok(tradeService.saveRecord(dto));
	}

	@DeleteMapping("/records/{id}")
	public Result saveSellRecord(@PathVariable("id") Long id) {
		SsTradeRecordDto dto = new SsTradeRecordDto();
		dto.setId(id);
		dto.setStatus(RecordStatus.DELETED.getValue());
		return Result.ok(tradeService.saveRecord(dto));
	}
}
